/**
 * 
 */
package com.google.code.cisx.converters;

import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
 * @since 1.0
 * @version $Id: ConvertUtilsBeanExtended.java 131 2008-01-20 07:48:47Z
 *          rahul.thakur.xdev $
 */
public class ConvertUtilsBeanExtended extends ConvertUtilsBean {

  /**
   * Commons Logging.
   */
  private static final Log log = LogFactory.getLog(ConvertUtilsBeanExtended.class);

  /**
   * 
   */
  public ConvertUtilsBeanExtended() {
    // do nothing
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.commons.beanutils.ConvertUtilsBean#convert(java.lang.Object,
   *      java.lang.Class)
   */
  @Override
  public Object convert(Object value, Class targetType) {
    // TODO Auto-generated method stub
    return super.convert(value, targetType);
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.commons.beanutils.ConvertUtilsBean#convert(java.lang.String,
   *      java.lang.Class)
   */
  @Override
  public Object convert(String value, Class clazz) {
    // TODO Auto-generated method stub
    return super.convert(value, clazz);
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.commons.beanutils.ConvertUtilsBean#convert(java.lang.String[],
   *      java.lang.Class)
   */
  @Override
  public Object convert(String[] values, Class clazz) {
    // TODO Auto-generated method stub
    return super.convert(values, clazz);
  }

  /**
   * 
   * @param value
   * @return
   */
  public Object convertAsObject(Object value) {
    if (value == null) {
      return null;
    } else if (value.getClass().isArray()) {
      if (Array.getLength(value) < 1) {
        return (null);
      }
      value = Array.get(value, 0);
      if (value == null) {
        return ((String) null);
      } else {
        Converter converter = lookup(String.class);
        return ((String) converter.convert(String.class, value));
      }
    } else if (value instanceof List || value instanceof ArrayList) {
      List retList = new ArrayList(); // return value
      List lValue = (List) value;
      if (lValue.size() < 1) {
        log.warn("Empty value List encountered. Returning empty converted List.");
        return retList;
      }
      // Only one type of elements will be there in the list.
      Class klass = lValue.get(0).getClass();
      // create a new list and convert all elements
      for (Iterator it = lValue.iterator(); it.hasNext();) {
        Converter converter = lookup(klass);
        if (null != converter)
          retList.add(converter.convert(klass, it.next()));
        else {
          log.warn("No Converter implementation registered for class: " + klass.getName()
              + ". No converted value could be added.");
        }
      }
      return retList;
    } else {
      // Converter converter = lookup(String.class);
      // return ((String) converter.convert(String.class, value));
      Converter converter = lookup(value.getClass());
      if (null == converter)
        return super.convert(value);
      else
        return converter.convert(value.getClass(), value);
    }
  }
}
